package com.lemon.smartsafety.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.utils.CommonUtil;


/**
 * 项目名称：${project_name}
 * 类名称：${type_name}
 * 类描述：
 * 创建人：Frago...
 * 创建时间：${date} ${time}
 * 修改人：${user}
 * 修改时间：${date} ${time}
 * 修改备注：
 */
public class ViewSearch extends LinearLayout {

    private Button btnLeft;
    private TextView txtClearn,txtRight,txtLine;
    private EditText editText;
    private RelativeLayout reyAll;

    public ViewSearch(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ViewSearch(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_search, this);

        txtClearn = (TextView) findViewById(R.id.txtClearn);
        txtRight = (TextView) findViewById(R.id.txtRight);
        txtLine = (TextView) findViewById(R.id.txtLine);


        editText = (EditText) findViewById(R.id.editText);
        reyAll = (RelativeLayout) findViewById(R.id.reyAll);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                if(!CommonUtil.strIsEmpty(editText.getText().toString().trim())){
                    txtClearn.setVisibility(VISIBLE);
                }else{
                    txtClearn.setVisibility(INVISIBLE);
                }

            }
        });


        txtClearn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                editText.setText("");
            }

        });


    }



    /**
     * 设置背景图片资源
     */
    public void setBackgroundResource(int resId) {
        reyAll.setBackgroundResource(resId);
    }


    public void setEditHint(String text) {
        editText.setHint(text);
    }

    /**
     * 设置显示的文字
     */
    public void setRightText(String text) {
        txtRight.setText(text);
        txtRight.setVisibility(VISIBLE);
    }

    /**
     * 设置右按钮的文字
     * @return
     */
    public String getRightText() {

        return txtRight.getText().toString().trim();
    }

    /**
     * 设置右按钮的文字颜色
     * @param color
     */
    public void getRightTextColor(int color) {

        txtRight.setTextColor(getResources().getColor(color));
    }


    public void setLineVisible(boolean visible){
        if(visible){
            txtLine.setVisibility(VISIBLE);
        }else{
            txtLine.setVisibility(GONE);
        }

    }



    public interface CallBackInterface {
        public void callBackFunction(View v);

    }

    /**
     * 设置按钮监听
     * @param callBackInterface
     */
    public void setSearchBack(final CallBackInterface callBackInterface) {

        txtRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                callBackInterface.callBackFunction(view);
            }

        });

    }


    public String getEdtContent(){
        return editText.getText().toString().trim();
    }




}